package com.sgb.goods.view.fragment.inquiry;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsFragmentMyInquiryBinding;
import com.sgb.goods.utils.base.fragment.BaseFragment;
import com.sgb.goods.utils.helper.SmartRefreshLayoutHelper;
import com.sgb.goods.viewmodel.inquiry.MyInquiryModel;
import com.sgb.goods.viewmodel.inquiry.MyInquiryViewModel;

import java.util.Objects;

/**
 * 描述：个人中心--我的询价
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.10.28
 */
public class MyInquiryFragment extends BaseFragment<GoodsFragmentMyInquiryBinding, MyInquiryViewModel>
        implements SmartRefreshLayoutHelper {

    private static final String KEY_DATATYPE = "DATA_TYPE";
    private int dataType = 0;
    private String searchPriceCheckName = "";
    private MyInquiryModel mViewModel2;

    public static MyInquiryFragment newInstance(int tabType) {
        MyInquiryFragment fragment = new MyInquiryFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_DATATYPE, tabType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_fragment_my_inquiry;
    }

    @Override
    protected void init() {
        DividerItemDecoration divider = new DividerItemDecoration(requireActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(requireActivity(), R.drawable.shape_list_divide_1)));
        mDataBinding.rlvLayout.rlvView.addItemDecoration(divider);
        assert getArguments() != null;
        dataType = getArguments().getInt(KEY_DATATYPE);
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(MyInquiryViewModel.class, BR.viewModel);
        mViewModel2 = createParentViewModel(MyInquiryModel.class);

    }

    @Override
    protected void initObserve() {
        mViewModel.priceCheckListEntityObservableField.observe(this, priceCheckListEntities -> {
            refreshUi(mDataBinding.rlvLayout.refreshLayout, mViewModel.myInquiryAdapter, priceCheckListEntities);
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.rlvLayout.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getPriceCheckList(searchPriceCheckName, dataType, false,false);
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.getPriceCheckList(searchPriceCheckName, dataType, true,false);
            }
        });
        // 解决 activity 软件盘收起时搜索内容未更新问题
        mViewModel2.bindSearchContent.observe(this, bindSearchContent -> {
            if (!bindSearchContent.equals(mViewModel2.searchContent.getValue())) {
                this.searchPriceCheckName = bindSearchContent;
            }
        });
        // activity 页面内容变更回调通知与处理
        mViewModel2.searchContent.observe(this, searchContent -> {
            // 为所有显示与未显示的 fragment 页面进行搜索内容变量覆盖
            this.searchPriceCheckName = searchContent;
            // 当前 fragment 显示，则调用网络请求带搜索内容的数据
            if (Lifecycle.Event.ON_RESUME == currentLifecycleEvent) {
                loadData(true);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData(false);
    }

    @Override
    protected void lazyLoadData() {
        loadData(true);
    }

    private void loadData(boolean isShowDialog) {
        mViewModel.getPriceCheckList(searchPriceCheckName, dataType, false,isShowDialog);
    }

}
